/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hangmangame;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Joana
 */
public class HangManFrameView extends AbstractHangManView implements IHangManView {

    protected HangManFrame frame;

    public HangManFrameView() {
        this.frame = new HangManFrame(this);
        this.frame.setVisible(true);
    }

    @Override
    public String drawText() {
        String line = super.drawText();
        this.frame.getAnswereLabel().setText(line);
        return line;
    }

    @Override
    public void drawQuestion() {
        if (!frame.getGuessField().getText().isEmpty()) {
            frame.getResultLabel().setText("Inserted character: " + frame.getGuessField().getText().charAt(0));
            if (game.addGuess(frame.getGuessField().getText().charAt(0))) {
                frame.getPlayResultLabel().setText("Good guest!");
            } else {
                frame.getPlayResultLabel().setText("Bad guest, number o trys available: " + game.getTrys());
                paintImage();
            }
        }
        frame.getGuessField().setText("");
    }

    @Override
    public void drawEndResult(int exitCode) {
        switch (exitCode) {
            case 1:
                frame.getResultLabel().setText("You win");
                break;
            case 2:
                frame.getResultLabel().setText("You lost, the word was: " + game.getWord());
        }
        frame.getPlayResultLabel().setText("");
    }

    @Override
    public boolean drawAskForNewGame() {
        Object[] options = {"Yes, please",
                    "No, thanks"};
        String game_result = "";
        switch(game.isEnd()) {
            case 1:
                game_result = "win";
                break;
            case 2:
                game_result = "lost";
                break;
        }
        
        int n = JOptionPane.showOptionDialog(frame, "You " + game_result + ", Play again?", "Play again?",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
            null, options, options[1]);       

        return (n == 0);
    }

    @Override
    public void drawEnd() {
    }

    @Override
    public void setGame(HangManGame game) {
        this.game = game;
    }

    @Override
    public void display() {
        newGame();
        paintImage();
        drawText();
        frame.getResultLabel().setText("");
        frame.getPlayResultLabel().setText("");
        frame.getGuessField().setText("");
    }

    @Override
    public void start() {
    }
    
    public void resetGame() {
        display();
    }
    
    public void checkStatus() {
        drawQuestion();
        drawText();
        if (game.isEnd() > 0) {
            drawEndResult(game.isEnd());
            if (drawAskForNewGame()) {
                display();
            } else {
                drawEnd();
            }
        }
    }
    
    protected void paintImage() {
        try {
          BufferedImage image = ImageIO.read(new File("Imagens/" + (game.getTrys() + 1) + ".jpg"));
          frame.getImageHangMan().setIcon(new ImageIcon(image));
          frame.getImageHangMan().setText("");
       } catch (IOException ex) {
            // handle exception...
       }
    }

}
